/**
 * 设置PLC参数
 * @author xiaoxujs
 */

import { Form, Input, message, Modal } from "antd";
import { FC, useEffect, useState } from "react";
import { postTlplTLaneRtspParamUpdate } from "./api";

const SetZBModule: FC<{
  open: boolean,
  handleSetPlcCallback: () => void;
  clickData?: string,
  laneId?: string;
}> = ({
  open,
  handleSetPlcCallback,
  clickData,
  laneId
}) => {

    const [form] = Form.useForm<{ rtspAddr: string }>();

    const [loading, setLoading] = useState(false);

    useEffect(() => {
      if (clickData) {
        form.setFieldsValue({rtspAddr: clickData})
      }
    }, [clickData, form])
    const handleOk = async () => {
      const values = await form.validateFields();
      console.log('values', values);
      if ( laneId) {
        setLoading(true);
        const { code, msg } = await postTlplTLaneRtspParamUpdate({
          ...values,
          id: laneId,
        }) as { code: number; msg: string };
        if (code === 20000) {
          form.resetFields();
          handleSetPlcCallback();
          
        } else {
          message.error(msg)
        }
        setLoading(false)

      }

    };

    const handleCancel = () => {
      handleSetPlcCallback();
    };
    return <Modal title="设置直播参数" confirmLoading={loading} open={open} onOk={handleOk} onCancel={handleCancel}>
      <Form
        labelCol={{ span: 8 }}
        wrapperCol={{ span: 14 }}
        form={form}
      >
        <Form.Item<{ rtspAddr: string }>
          label="RTSP地址"
          name="rtspAddr"
          rules={[{ required: true, message: '请输入!' }]}
        >
          <Input />
        </Form.Item>

      </Form>
    </Modal>
  }

export default SetZBModule;